package org.com.ashen.service.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.com.ashen.service.service.smssendlogmanage.sms.inter.http.v1.dto.response.SmsResponse;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class SysWM {

    @Test
    public void testGetParams() {
//        // 短信模板
//        String template = "${userName}，您好！您的验证码是：${code}，还有${min}分钟过期！";
//
//        // 正则匹配参数
//        Pattern pattern = Pattern.compile("[$][\\{]([^$]*?)[\\}]");
//        Matcher matcher = pattern.matcher(template);
//        while (matcher.find()) {
//            System.out.println(matcher.group(1));
//        }

//        Date sendTime = null;
////        try {
////            sendTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format("2022-11-08 15:19:16");
////        }catch (Exception e){}
////        System.out.println(sendTime);


//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String date = null;
//        try {
//            date = simpleDateFormat.format(new Date());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println(date);


        List<String> smsids = new ArrayList<String>();
        String[] smsidss = null;
       smsids.add("aaa");
       smsids.add("abb");
         smsidss = smsids.toArray(new String[smsids.size()]);
        System.out.println(Arrays.toString(smsidss));
        String mobiles = Arrays.toString(smsidss);
        mobiles = "[\""+mobiles+"\"]";
        System.out.println(mobiles);
        mobiles = mobiles.replace("[\"[","").replace("]\"]","");
        System.out.println(mobiles);
        smsidss = mobiles.split(",");
        System.out.println(Arrays.toString(smsidss));
        String outputStr = "userId=56292e48&mobiles="+ Arrays.toString(smsidss)+"&customSmsId=上卧&templateCode=98KJUI08&smscontent=aa&sendTime=";
        System.out.println(outputStr);
    }


    @Test
    public void aaa() {
        String tempalteContent = "您的“${CarBrand}”(车牌$CarPlateNum$)已经完成租用(订单号：${orderNo})。本次租金收益${RentIncome}元，${ServiceExpense}$ServiceExpenseProxy$$You2RenterAdjust$$Renter2YouAdjust$$HardwareDeposit$$ownerTransDebt$$TotalIncome$在1个工作日后结算至您设置的银行卡中。您可在App车主收益页面查看详情";
//        Map contentParamMap = new HashMap();
//        contentParamMap.put("CarBrand", "奥迪");
//        contentParamMap.put("CarPlateNum", "沪A0001");
//        contentParamMap.put("orderNo", "1245221158");
//        contentParamMap.put("RentIncome", "2000");
//        contentParamMap.put("ServiceExpense", "平台服务费100元，");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("CarBrand","奥迪");
        jsonObject.put("CarPlateNum","沪A0001");
        jsonObject.put("orderNo","1245221158");
        jsonObject.put("RentIncome","2000");
        jsonObject.put("ServiceExpense","平台服务费100元");

//        System.out.println(JSON.toJSONString(contentParamMap));
        Scanner sc = new Scanner(tempalteContent);
        StringBuffer buf = new StringBuffer();
        try{
            Pattern p = Pattern.compile("[$][\\{]([^$]*?)[\\}]");
            while (sc.hasNext()) {
                System.out.println(sc.toString());
                Matcher m = p.matcher(sc.nextLine());
                while (m.find()) { //查找并替换参数
                    System.out.println(m.group(1));
                    //从map中根据key获取值
                    m.appendReplacement(buf, jsonObject.get(m.group(1)) != null ? jsonObject.get(m.group(1)).toString() : "");
                }
                m.appendTail(buf);
            }
        }catch(Exception e){
//            logger.error("替换短信模板内容报错！",e);
            e.printStackTrace();
        }finally{
            sc.close();
        }
        System.out.println("buf=" + buf);

    }

}